package com.smartpro.mis.modular.business.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.smartpro.mis.core.base.controller.BaseController;
import com.smartpro.mis.rest.modular.auth.controller.dto.ResponseBean;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;

import com.smartpro.mis.modular.system.model.Product;
import com.smartpro.mis.modular.system.model.ProductClass;
import com.smartpro.mis.modular.business.service.IProductClassService;
import com.smartpro.mis.modular.business.service.IProductService;

/**
 * 商品分类控制器
 *
 * @author Mengiy
 * @Date 2019-04-10 16:55:59
 */
@RestController
@RequestMapping("/api/service/productClass")
public class ProductClassController extends BaseController {


    @Autowired
    private IProductClassService productClassService;
    
    @Autowired
    private IProductService productService;
    
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 获取商品分类列表
     */
    @ApiOperation(value ="获取商品分类列表")
    @RequestMapping(value = "/list")
    @ResponseBody
    @RequiresAuthentication
    public Object list() {
    	List<ProductClass> productClasses=productClassService.getListOrderBY();
    	for(ProductClass c:productClasses) {
    		EntityWrapper<Product> wrapper=new EntityWrapper<Product>();
    		
    		wrapper.where("classID = {0}",c.getClassID());
    		c.setNum(productService.selectCount(wrapper));
    	}
        return  new ResponseBean(200,"query success",productClasses);
    }
    /**
     * 获取商品分类列表
     */
    @ApiOperation(value ="获取商品分类列表")
    @RequestMapping(value = "/listFast")
    @ResponseBody
    @RequiresAuthentication
    public Object listFast() {
    	List<ProductClass> productClasses=productClassService.getListOrderBY();
        return  new ResponseBean(200,"query success",productClasses);
    }
    /**
     * 新增商品分类
     */
    @ApiOperation(value = "新增商品分类",notes = "添加商品分类")
    @ApiImplicitParam(name = "productClass",value = "商品分类的详细信息",required = true,dataType = "JSONObject")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    @RequiresAuthentication
    public Object add(@RequestBody ProductClass productClass) {
    	productClass.setCreateTime(sdf.format(new Date()));
        productClassService.insert(productClass);
        return SUCCESS_TIP;
    }

    /**
     * 删除商品分类
     */
    @RequestMapping(value = "/delete/{productClassId}",method = RequestMethod.GET)
    @ApiOperation(value = "删除商品分类",notes = "根据编号进行删除")
    @ApiImplicitParam(name = "productClassId",value = "商品分类编号",required = true,dataType = "Integer")
    @ResponseBody
    @RequiresAuthentication
    public Object delete(@PathVariable("productClassId") Integer productClassId) {
        productClassService.deleteById(productClassId);
        return SUCCESS_TIP;
    }

    /**
     * 修改商品分类
     */
    @ApiOperation(value = "修改商品分类",notes = "修改商品分类信息")
    @ApiImplicitParam(name = "productClass",value = "商品分类的详细信息",required = true,dataType = "ProductClass")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    @RequiresAuthentication
    public Object update(@RequestBody ProductClass productClass) {
        productClassService.updateById(productClass);
        return SUCCESS_TIP;
    }

    /**
     * 商品分类详情
     */
    @RequestMapping(value = "/detail/{productClassId}")
    @ResponseBody
    public Object detail(@PathVariable("productClassId") Integer productClassId) {
        return productClassService.selectById(productClassId);
    }
}
